﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.IO;

namespace SynoManager.DownloadStation.Plugins
{
    [DownloadStationPlugin("Folder Watcher", "Watches a folder for processable files", "Grad van Horck", "1.0")]
    public class FolderWatcher : IDownloadStationPreferencePanelPlugin
    {
        private DownloadStationService service;

        private string path = Properties.Settings.Default.FolderWatcherPath; 
        private bool deleteOnAccept = Properties.Settings.Default.FolderWatcherDeleteOnAccept;

        private FileSystemWatcher watcher = new FileSystemWatcher();

        public FolderWatcher()
        {
            watcher.EnableRaisingEvents = false;
            watcher.Created += watcher_Created;
            Path = path;
        }

        public string Path
        {
            get
            {
                return path;
            }
            set
            {
                path = value;
                Properties.Settings.Default.FolderWatcherPath = value;
                if (Directory.Exists(path))
                {
                    watcher.Path = path;
                    watcher.EnableRaisingEvents = true;
                }
                else
                {
                    watcher.EnableRaisingEvents = false;
                }
            }
        }

        public bool DeleteOnAccept
        {
            get { return deleteOnAccept; }
            set
            {
                deleteOnAccept = value;
                Properties.Settings.Default.FolderWatcherDeleteOnAccept = value;
            }
        }

        void watcher_Created(object sender, FileSystemEventArgs e)
        {
            Debug.Assert(service != null, "Service could not be found");
            Debug.WriteLine("Folderwatcher detected new file: " + e.Name, "FolderWatcher");
            if (service == null)
                return;

            service.AddFile(e.FullPath);

            if (DeleteOnAccept)
                File.Delete(e.FullPath);
        }

        public void Initialize(DownloadStationService service)
        {
            if (Directory.Exists(Path))
            {
                watcher.Path = Path;
                watcher.EnableRaisingEvents = true;
                this.service = service;
            }
            else
            {
                watcher.EnableRaisingEvents = false;
                this.service = service;
            }
        }

        public void Close()
        {
            service = null;
        }

        #region IDownloadStationPreferencePanelPlugin Members

        public SynoManager.GUI.PreferencesPanel PreferencePanel
        {
            get { return new FolderWatcherOptionsPanel(this); }
        }

        public string PreferenceNodeText
        {
            get { return "Folder Watcher"; }
        }

        public System.Drawing.Image PreferenceNodeImage
        {
            // TODO: Add Image 
            get
            {
                return null; 
            }
        }

        #endregion
    }
}
